import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Put,
  Delete,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { PostService } from './post.service';
import {
  CreatePostDto,
  UpdatePostDto,
  CreatePostCommentDto,
} from './dto/create-post.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('api/post')
export class PostController {
  constructor(private readonly postService: PostService) {}

  @UseGuards(JwtAuthGuard)
  @Post()
  async create(@Body() createPostDto: CreatePostDto, @Request() req) {
    const post = await this.postService.create(createPostDto, req.user.sub);
    return {
      code: 200,
      message: '发布成功',
      data: post,
    };
  }

  @UseGuards(JwtAuthGuard)
  @Get('list')
  async findAll(@Query() query, @Request() req) {
    const [posts, total] = await this.postService.findAll(query, req.user.sub);
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: posts,
        total,
      },
    };
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    const post = await this.postService.findOne(+id);
    return {
      code: 200,
      message: '获取成功',
      data: post,
    };
  }

  @UseGuards(JwtAuthGuard)
  @Put(':id')
  async update(
    @Param('id') id: string,
    @Body() updatePostDto: UpdatePostDto,
    @Request() req,
  ) {
    const post = await this.postService.update(
      +id,
      updatePostDto,
      req.user.sub,
    );
    return {
      code: 200,
      message: '更新成功',
      data: post,
    };
  }

  @UseGuards(JwtAuthGuard)
  @Delete(':id')
  async remove(@Param('id') id: string, @Request() req) {
    await this.postService.remove(+id, req.user.sub);
    return {
      code: 200,
      message: '删除成功',
    };
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/like')
  async like(@Param('id') id: string, @Request() req) {
    const post = await this.postService.like(+id, req.user.sub, req.user.nickname);
    return {
      code: 200,
      message: '点赞成功',
      data: post,
    };
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/unlike')
  async unlike(@Param('id') id: string, @Request() req) {
    const post = await this.postService.unlike(+id, req.user.sub);
    return {
      code: 200,
      message: '取消点赞成功',
      data: post,
    };
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/comment')
  async createComment(
    @Param('id') id: string,
    @Body() createCommentDto: CreatePostCommentDto,
    @Request() req,
  ) {
    const comment = await this.postService.createComment(
      +id,
      createCommentDto,
      req.user.sub,
      req.user.nickname,
    );
    return {
      code: 200,
      message: '评论成功',
      data: comment,
    };
  }

  @UseGuards(JwtAuthGuard)
  @Post('my-posts')
  async getMyPosts(@Request() req) {
    const posts = await this.postService.getMyPosts(req.user.sub);
    return {
      code: 200,
      message: '获取成功',  
      data: posts,
    };
  }
  //获取我的所有评论
  @UseGuards(JwtAuthGuard)
  @Post('my-comments')
  async getMyComments(@Request() req){
    const comments = await this.postService.getMyComments(req.user.sub);
    return {
      code: 200,
      message: '获取成功',
      data: comments,
    }
  }

  //获取我的所有点赞过的帖子
  @UseGuards(JwtAuthGuard)
  @Post('my-likes')
  async getMyLikedPosts(@Request() req){
    const posts = await this.postService.getMyLikedPosts(req.user.sub);
    return {
      code: 200,
      message: '获取成功',
      data: posts,
    }
  }
}
